package com.huijutao.activityContent;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.view.MenuItemCompat;
import android.support.v7.app.ActionBarActivity;
import android.support.v7.widget.Toolbar;
import android.view.ActionProvider;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ImageView;
import android.support.v7.widget.ShareActionProvider;
import android.widget.TextView;
import android.widget.Toast;

import com.huijutao.MyApp;
import com.huijutao.R;
import com.huijutao.beans.FindItem;
import com.lidroid.xutils.db.sqlite.Selector;
import com.lidroid.xutils.db.sqlite.WhereBuilder;
import com.lidroid.xutils.exception.DbException;

import java.util.List;


public class FindActivity extends ActionBarActivity {
    public static final String DATA = "data";
    private FindItem findItem;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_find);
        Toolbar toolbar= (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        //设置Up按钮
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        //获取传过来的要显示的商品的内容
        findItem = (FindItem) getIntent().getSerializableExtra(DATA);
        //设置显示的内容
        ImageView img = (ImageView) findViewById(R.id.img);
        TextView txt_title = (TextView) findViewById(R.id.txt_title);
        TextView txt_price = (TextView) findViewById(R.id.txt_price);
        TextView txt_intro = (TextView) findViewById(R.id.txt_intro);
        MyApp.mImageXutils.display(img, findItem.getImg_path());
        txt_title.setText(findItem.getTitle());
        txt_price.setText("¥" + findItem.getPrice());
        txt_intro.setText(findItem.getIntro());
        //设置去购买的点击事件
        findViewById(R.id.btn).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Uri uri = Uri.parse(findItem.getGo_url());
                Intent it = new Intent(Intent.ACTION_VIEW, uri);
                startActivity(it);
            }
        });

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu_findactivity,menu);
        MenuItem item=menu.findItem(R.id.menu_shouchang);
        try {
            List<FindItem> findItems = MyApp.dbUtils.findAll(Selector.from(FindItem.class).where("title", "=", findItem.getTitle()));
            if (findItems!=null&&findItems.size()>0){
                item.setIcon(R.drawable.bj_chk_collect_checked);
            }
        } catch (DbException e) {
            e.printStackTrace();
        }
        MenuItem share = menu.findItem(R.id.menu_findactivity_share);
        ShareActionProvider actionProvider = (ShareActionProvider) MenuItemCompat.getActionProvider(share);
        Intent intent = new Intent(Intent.ACTION_SEND);
        intent.setType("text/*");
        intent.putExtra(Intent.EXTRA_TITLE,"分享");
        intent.putExtra(Intent.EXTRA_TEXT,findItem.getTitle());
        actionProvider.setShareIntent(intent);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        try {
            List<FindItem> findItems = MyApp.dbUtils.findAll(Selector.from(FindItem.class).where("title", "=", findItem.getTitle()));

            int id = item.getItemId();
            if (id == android.R.id.home) {
                this.finish();
            }else if (id==R.id.menu_shouchang) {
                if (findItems==null||findItems.size()==0) {
                    item.setIcon(R.drawable.bj_chk_collect_checked);
                    Toast.makeText(this,"收藏成功",Toast.LENGTH_SHORT).show();
                    MyApp.dbUtils.saveOrUpdate(findItem);
                }else {
                    item.setIcon(R.drawable.bj_chk_collect_default);
                    Toast.makeText(this,"取消收藏成功",Toast.LENGTH_SHORT).show();
                    MyApp.dbUtils.delete(FindItem.class, WhereBuilder.b("title","=",findItem.getTitle()));
                }
            }
        } catch (DbException e) {
            e.printStackTrace();
        }
        return false;
    }
}
